package org.whale.springcloud.eureka.user.client.service.impl;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.whale.springcloud.eureka.common.entity.JsonResponse;
import org.whale.springcloud.eureka.user.client.config.UserClientConfig;
import org.whale.springcloud.eureka.user.client.service.UserService;

@Service
public class UserServiceImpl implements UserService {

	private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

	@Autowired
	private RestTemplate restTemplate;

	@Autowired
	private UserClientConfig userClientConfig;

	@Override
	public JsonResponse<String> queryUserInfo(Map<String, String> params) {

		String url = "http://"
				+ userClientConfig.getServerIp()
				+ ":"
				+ userClientConfig.getServerPort()
				+ "/userServer/queryUserInfo";

		logger.info("start query user info : ");
		logger.info("query user info params : " + params.toString());
		logger.info("query user info url : " + url);

		ParameterizedTypeReference<JsonResponse<String>> typeRef = new ParameterizedTypeReference<JsonResponse<String>>() {
		};
		JsonResponse<String> result = restTemplate.exchange(url, HttpMethod.GET, null, typeRef)
				.getBody();

		logger.info("query user info result : " + result.getMessage());

		return result;
	}
}
